knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
Create a test-function and run it:
testfun <- function () {}
testfun() # NULL as this function doesn't give an output
## NULL
class(testfun) # It is an element of class "function"
## [1] "function"
How to specify what the function does (use {})
testfun <- function () {print("this function does nothing")}
testfun()
## [1] "this function does nothing"
my_age <- function(birthday, units) {
difftime(Sys.time(), birthday, units = units)
}
my_age("1991-11-16", units = "days")
## Time difference of 10782.53 days
# Use default values:
my_age <- function(birthday, units = "days") {
difftime(Sys.time(), birthday, units = units)
}
my_age("1991-11-16")
## Time difference of 10782.53 days
my_age("1991-11-16", units = "hours") # Still, other units than default are possible.
## Time difference of 258780.7 hours
Creating a function to taoculate the euclidian distance
euclidian_dist <- function(E.x, E.y, N.x, N.y) {
sqrt((E.x-E.y)^2 + (N.x-N.y)^2)
}
euclidian_dist_lead <- function(E, N) {
sqrt((E-lead(E))^2 + (N-lead(N))^2)
}
Import Dataset and filter it: We will only Use the individuals Rosa and Sabi for the timespan of 01.04.2015 - 15.04.2015
raw <- read_delim("wildschwein_BE_2056.csv", delim = ",")
##
## -- Column specification --------------------------------------------------------
## cols(
## TierID = col_character(),
## TierName = col_character(),
## CollarID = col_double(),
## DatetimeUTC = col_datetime(format = ""),
## E = col_double(),
## N = col_double()
## )
summary(raw)
## TierID TierName CollarID
## Length:51246 Length:51246 Min. :12275
## Class :character Class :character 1st Qu.:12275
## Mode :character Mode :character Median :13972
## Mean :13219
## 3rd Qu.:13974
## Max. :13974
## DatetimeUTC E N
## Min. :2014-08-22 21:00:12 Min. :2568153 Min. :1202306
## 1st Qu.:2015-02-20 12:18:57 1st Qu.:2569511 1st Qu.:1204825
## Median :2015-04-08 20:00:10 Median :2569964 Median :1205210
## Mean :2015-03-24 23:09:22 Mean :2570330 Mean :1205227
## 3rd Qu.:2015-06-01 05:45:13 3rd Qu.:2570465 3rd Qu.:1206005
## Max. :2015-07-27 11:00:14 Max. :2575154 Max. :1207609
data.filter <- raw %>%
filter(TierName == "Sabi" | TierName =="Rosa",
DatetimeUTC >= as.POSIXct("2015-04-01", tz = "UTC"),
DatetimeUTC <= as.POSIXct("2015-04-15", tz = "UTC"))
Crate a rouded data for every 15 minutes => This will be the join key.
data.filter$DatetimeUTC <- round_date(data.filter$DatetimeUTC, unit = "15 minutes")
Create two datasets (one per animal), and join the datasets by DateTimeUTC. Calculate the euclidian distance between the two individuals, using the previously definded function.
data.filter.sabi <- filter(data.filter, TierName == "Sabi")
data.filter.rosa <- filter(data.filter, TierName == "Rosa")
data.join <- left_join(data.filter.sabi, data.filter.rosa, by = "DatetimeUTC", suffix = c("Sabi", "Rosa"))
data.join$distance <- euclidian_dist(data.join$ESabi, data.join$ERosa, data.join$NSabi, data.join$NRosa)
data.join$meeting <- ifelse(data.join$distance <=100, TRUE, FALSE)
# Filter and gather the relevant data for visuailzation:
data.join.vis <- data.join %>%
select(DatetimeUTC, ERosa, NRosa, ESabi, NSabi, distance, meeting)
data.join.vis <- gather(data.join.vis, TierE, E, ERosa, ESabi)
data.join.vis <- gather(data.join.vis, TierN , N, NRosa, NSabi)
data.join.vis$Tier <- ifelse(data.join.vis$TierE == "ERosa", "Rosa", "Sabi")
ggplot(filter(data.join.vis, distance < 3000), aes (E, N, fill = Tier, col = meeting)) +
geom_point(alpha = 0.30, shape = 21) +
labs() +
scale_color_manual(values = c("white", "black")) +
theme_bw()
Die Tiere haben sich einige Male an jeweils ähnichen Orten getroffen. Zwecks übersichtlichkeit werden im obigen Plot nur Standorte mit einer Distanz von < 3000 m zwischen den Tieren abgebildet.
plot_ly(data.join.vis, x = ~E, y = ~N, z = ~DatetimeUTC, type = "scatter3d", mode = "lines", color = ~Tier)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels